import { ajax } from "../../utils/index";

Component({
  properties:{
    data:Object
  },

  data:{
    iscollected: false,
    collectionIcon: ['../../images/mycollect.png', '../../images/收藏_实心.png']
  },

  lifetimes:{
    attached: async function(){
      const params = {
        id: this.properties.data.id,
        openid:wx.getStorageSync('openid'),
      };
      const result = await ajax('/checkCollection','POST',params);
      const {data} = result;
      if(data.length > 0){
        let collectionIcon = this.data.collectionIcon;
        let iscollected = !this.data.iscollected;
        let last = collectionIcon.pop();
        collectionIcon.unshift(last);
        this.setData({
          iscollected,
          collectionIcon,
        })
      }
    }
    
  },

  methods:{
    toCollection() {  
      let collectionIcon = this.data.collectionIcon;
      let iscollected = !this.data.iscollected;
      let last = collectionIcon.pop();
      collectionIcon.unshift(last);
      this.setData({
        iscollected,
        collectionIcon,
      })
      this.triggerEvent('getiscollected',iscollected);
  },
  }
})